package com.justrun.zjutsystem.zjutsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.justrun.response.ResultCodeEnum;
import com.justrun.response.ResultEntity;
import com.justrun.utils.ExcelUtils;
import com.justrun.zjutsystem.zjutsystem.dtos.AddClassDto;
import com.justrun.zjutsystem.zjutsystem.entity.AdministrativeClass;
import com.justrun.zjutsystem.zjutsystem.entity.College;
import com.justrun.zjutsystem.zjutsystem.service.AdministrativeClassService;
import com.justrun.zjutsystem.zjutsystem.service.CollegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学院信息表 前端控制器
 * </p>
 *
 * @author chandler
 * @since 2020-09-17
 */
@Slf4j
@Api("学院班级管理")
@RestController
@RequestMapping("/zjutsystem/collegeClass")
@CrossOrigin
public class CollegeController {

    @Autowired
    private CollegeService collegeService;
    @Autowired
    private AdministrativeClassService classService;

    @ApiOperation("获取学院班级列表")
    @GetMapping("/getCollegeList")
    public ResultEntity getClassList(@RequestParam(value = "collegeName",required = false) String collegeName,
                                     @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                                     @RequestParam(value = "pageSize",defaultValue = "6") Integer pageSize){

        return collegeService.getCollegeList(collegeName,pageIndex,pageSize);
    }

    @ApiOperation("获取学院名称列表")
    @GetMapping("/getCollegeNames")
    public ResultEntity getCollegeNames(){
        return collegeService.getCollegeNames();
    }

    @ApiOperation("添加学院")
    @GetMapping("/addCollege")
    public ResultEntity addCollege(@RequestParam(value = "collegeId") String collegeId,
                                 @RequestParam(value = "collegeName") String collegeName ){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("college_id",collegeId);
        //学院已存在返回错误
        if (collegeService.getOne(wrapper) != null){
            return ResultEntity.fail().code(ResultCodeEnum.COLLEGE_CANNOT_READD.getCode())
                    .message(ResultCodeEnum.COLLEGE_CANNOT_READD.getMesssage());
        }
        College college = new College();
        college.setCollegeId(collegeId);
        college.setCollegeName(collegeName);
        if (collegeService.save(college)){
            return ResultEntity.ok().message("添加学院成功");
        }else {
            return ResultEntity.fail();
        }
    }

    @ApiOperation("添加班级")
    @PostMapping("/addClass")
    public ResultEntity addClass(@RequestBody AddClassDto addClassDto){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("class_id",addClassDto.getClassId());
        //班级已存在直接返回错误
        if (classService.getOne(queryWrapper) != null){
            return ResultEntity.fail().code(ResultCodeEnum.RECORD_ALREADY_EXEST.getCode())
                    .message(ResultCodeEnum.RECORD_ALREADY_EXEST.getMesssage());
        }
        queryWrapper.clear();
        queryWrapper.eq("college_id",addClassDto.getCollegeId());
        //班级所在学院不存在返回错误
        if (collegeService.getOne(queryWrapper) == null){
            return ResultEntity.fail().code(ResultCodeEnum.COLLEGE_NOT_ADD.getCode())
                    .message(ResultCodeEnum.COLLEGE_NOT_ADD.getMesssage());
        }
        AdministrativeClass administrativeClass = new AdministrativeClass();
        administrativeClass.setClassId(addClassDto.getClassId());
        administrativeClass.setClassName(addClassDto.getClassName());
        administrativeClass.setCollegeId(addClassDto.getCollegeId());
        administrativeClass.setCollegeName(addClassDto.getCollegeName());
        if (!classService.save(administrativeClass)){
             return ResultEntity.fail();
        }
        return ResultEntity.ok();
    }

    @ApiOperation("删除班级")
    @DeleteMapping("/removeClass")
    public ResultEntity removeClass(@RequestParam("className") String className){
        if (className == null || StringUtils.isBlank(className)){
            return ResultEntity.fail().code(ResultCodeEnum.REQUEST_PARAM_ERROR.getCode())
                    .message(ResultCodeEnum.REQUEST_PARAM_ERROR.getMesssage());
        }
        Map<String,Object> condition = new HashMap<>();
        condition.put("class_name",className);
        if (!classService.removeByMap(condition)){
            return ResultEntity.fail().message("删除失败");
        }
        return ResultEntity.ok();
    }

    @ApiOperation("批量导入学院班级")
    @PostMapping("/batchAdd")
    public ResultEntity batchAdd(@RequestParam("classFile") MultipartFile classFile){

        InputStream inputStream = null;
        //文件解析结果
        List<List<String>> resultList = null;
        try {
            inputStream = classFile.getInputStream();
            resultList = ExcelUtils.getRowsFromExcel(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件解析错误：{}",e.getCause().getMessage());
        }finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //解析结果为空返回文件解析错误
        if(resultList == null || resultList.size() < 1){
            return ResultEntity.fail().code(ResultCodeEnum.FILE_EXPLAIN_ERROR.getCode())
                    .message(ResultCodeEnum.FILE_EXPLAIN_ERROR.getMesssage());
        }
        return collegeService.batchAdd(resultList);
    }
}

